//
//  Z_PopView.m
//  UGuang
//
//  Created by Lidear on 16/3/23.
//  Copyright © 2016年 LidearOceanus. All rights reserved.
//

#import "Z_PopView.h"

#define ZShowViewWidh 150
#define ZShowViewBtnHeight 30

@interface Z_PopView ()
@property (nonatomic, strong)NSMutableArray *arr;
@end


@implementation Z_PopView

+ (instancetype)sharePopView {
    static Z_PopView *player = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        player = [[Z_PopView alloc] init];
        
    });
    return player;
}
- (instancetype)initWithArray:(NSArray *)array
{
    
    self = [[Z_PopView alloc] initWithFrame:CGRectMake(0, 0, ZShowViewWidh*NORMWIDTH, NORMHEIGHT*ZShowViewBtnHeight*array.count)];
    [self createSubViews:array];
    self.backgroundColor = [UIColor whiteColor];
    self.layer.borderWidth = 1
    ;
    self.layer.masksToBounds = YES;
    self.layer.cornerRadius = 10.0;
    self.layer.borderColor =  [[UIColor blackColor] CGColor];
    return self;
}

// 创建items
- (void)createSubViews:(NSArray *)array
{
    self.arr = [NSMutableArray array];
    for (int i = 0; i < array.count; i ++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setFrame:CGRectMake(0, ZShowViewBtnHeight*i*NORMHEIGHT, ZShowViewWidh*NORMWIDTH, ZShowViewBtnHeight*NORMHEIGHT)];
        [btn setTitle:array[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(chooseItem:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
        btn.tag = 1211+i;
        [_arr addObject:btn];
    }
}

// 显示
- (void)showInView:(UIView *)view baseView:(UIView *)baseView withPosition:(ZShowPosition)position
{
    CGPoint point = baseView.frame.origin;
    CGSize size = baseView.frame.size;
    CGPoint center = baseView.center;
    switch (position) {
        case ZShowTop:
        {
            //  上
            self.center = CGPointMake(center.x, center.x - self.frame.size.height / 2.0 - size.height / 2.0);
            [self add_self:view];
        }
            break;
            
        case ZShowLeft:
        {
            //左
            self.frame = CGRectMake(point.x - size.width, point.y + size.height / 2.0, self.frame.size.width, self.frame.size.height);
            [self add_self:view];
        }
            
            break;
            
        case ZShowBottom:
        {
         //  下
            
          self.center = CGPointMake(center.x, center.y + self.frame.size.height / 2.0 + size.height / 2.0+self.i);
     //       self.frame = CGRectMake(point.x,  point.y + size.height / 2.0, self.frame.size.width, self.frame.size.height);
            [self add_self:view];
        }
            break;
            
        default:
        {
            // 右
            CGFloat a = point.y + size.height / 2.0+self.i;
            
            self.frame = CGRectMake(point.x ,a, self.frame.size.width, self.frame.size.height);;
            [self add_self:view];
        }
            break;
    }
}




- (void)add_self:(UIView *)view
{
    [view addSubview:self];
    [UIView animateWithDuration:.3 animations:^{
        self.alpha = 1.0;
    }];
}

// 选择item时回调
- (void)chooseItem:(id)sender
{
    
    
    UIButton *btn = (UIButton *)sender;
    NSString *str = [NSString stringWithFormat:@"%@,%ld",btn.titleLabel.text,btn.tag-1211];
    if (_chooseBlock) {
        _chooseBlock(str);
        [UIView animateWithDuration:.3 animations:^{
            self.alpha = 0;
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
        }];
    }
}

@end
